type ParameterError = {
  field?: string;
  message: string;
};

type ResponseError = ParameterError[];

export class HttpRespDto<T = any> {
  readonly code: number;
  readonly message: string;
  readonly data: T | null;
  readonly error: ResponseError | null;
  readonly timestamp: number;

  constructor(
    code: number,
    message: string,
    data?: T,
    error?: string | ResponseError,
  ) {
    this.code = code;
    this.message = message;
    this.data = data ?? null;
    this.error = error
      ? typeof error === 'string'
        ? [{ message: error }]
        : error
      : null;
    this.timestamp = Date.now();
  }

  static ok<U>(data?: U): HttpRespDto<U> {
    return new HttpRespDto(0, 'ok', data);
  }

  static fail(
    error: string | ParameterError[],
    code = -1,
    message = 'fail',
  ): HttpRespDto<null> {
    return new HttpRespDto(code, message, null, error);
  }
}
